/**
 * Copyright (c) 2004-2017 http://www.ibczy.com/
 * All rights reserved.
 * <p>
 * Crated by ant,  date:  2017/4/28   17:44
 * <p>
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 * <p>
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 * <p>
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.antt.cn.test2.uis.views;

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.util.AttributeSet;

import com.antt.cn.test2.utils.AntLog;

/**
 * Created by Ant on 2017/4/28  17:44
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class ReaderTextView extends android.support.v7.widget.AppCompatTextView {

    private int charIndex = 0;
    private String text = "";

    public ReaderTextView(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public ReaderTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public ReaderTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        resize();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }


    public void setData(String str) {
        if (null == str)
            return;
        text = str;
        charIndex=0;
        setText(text);
    }


    public void nextPage() {
      int pageSize=getCharNum();
      if((pageSize+charIndex)>=text.length()){
          AntLog.i("没有下一页了");
          return;
      }
      charIndex+=pageSize;
      setText(text.substring(charIndex));
    }

    public void prePage(){

        int pageSize=getCharNum();

    }


    /**
     * 去除当前页无法显示的字
     *
     * @return 去掉的字数
     */
    public int resize() {
        CharSequence oldContent = getText();
        CharSequence newContent = oldContent.subSequence(0, getCharNum());
        setText(newContent);
        int num = oldContent.length() - newContent.length();
        return num;
    }

    /**
     * 获取当前页总字数
     */
    public int getCharNum() {
        return getLayout().getLineEnd(getLineNum());
    }

    /**
     * 获取当前页总行数
     */
    public int getLineNum() {
        Layout layout = getLayout();
        int topOfLastLine = getHeight() - getPaddingTop() - getPaddingBottom() - getLineHeight();
        return layout.getLineForVertical(topOfLastLine);
    }

}
